package demo_table.util;

import demo_table.bean.ClassBean;
import demo_table.bean.StudentBean;


import java.util.*;

public class DataUtil {

    private  static Map<Integer, StudentBean> studentsInfo;
    private  static Map<Integer, ClassBean> classesInfo;
    //记录下一个学生的编号
    private static int nextId;

    private DataUtil(){

    }
    static{
       //初始化数据
        studentsInfo = new HashMap<Integer, StudentBean>();
        studentsInfo.put(1,new StudentBean(1,"李四","男",19,new ClassBean(1,"班级1")));
        studentsInfo.put(2,new StudentBean(2,"王五","女",20,new ClassBean(2,"班级2")));
        studentsInfo.put(3,new StudentBean(3,"张三","男",22,new ClassBean(1,"班级1")));
        studentsInfo.put(4,new StudentBean(4,"老刘","女",18,new ClassBean(2,"班级2")));
        studentsInfo.put(5,new StudentBean(5,"老赵","男",26,new ClassBean(1,"班级1")));
        studentsInfo.put(6,new StudentBean(6,"老周","男",20,new ClassBean(2,"班级2")));

        //保存下一个学生的编号
        nextId = studentsInfo.size()+1;

        classesInfo= new HashMap<Integer, ClassBean>();
        classesInfo.put(1,new ClassBean(1,"班级1"));
        classesInfo.put(2,new ClassBean(2,"班级2"));
    }

    /**
     * 获得所有的学生
     * @return 学生
     */
    public static  List<StudentBean> getAllStduent(){

          List<StudentBean> list = new ArrayList<StudentBean>();

          //循环map集合
        Set<Integer> keys = studentsInfo.keySet();

        for (Integer key : keys) {

            list.add(studentsInfo.get(key));
        }

        return list;
    }

    /**
     * 添加
     * @param studentBean 学生对象
     */
    public static void addStudent(StudentBean studentBean){

        studentsInfo.put(nextId,studentBean);

    }

    /**
     * 得到所有的班级
     * @return
     */
    public static  List<ClassBean> getAllClasses(){

        List<ClassBean> list = new ArrayList<ClassBean>();

        //循环map集合
        Set<Integer> keys = classesInfo.keySet();

        for (Integer key : keys) {

            list.add(classesInfo.get(key));
        }

        return list;
    }

    public static void deleteStudent(int id){
        studentsInfo.remove(id);
    }

    public static void updateStudent(int id, StudentBean student){
        studentsInfo.put(id,student);
    }

    public static StudentBean getStudentById(int id){
        return studentsInfo.get(id);
    }

    public static int getNextId(){
        return ++nextId;
    }


}
